package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.LocalMusicInfo;
import com.lly.songhigh.utils.ChangeUtils;

import java.util.List;

/**
 * Created by 李新伟 on 15/10/30.
 */
public class LocalMusicListAdapter extends BaseAdapter {

    private Context context;
    private List<LocalMusicInfo> localMusicInfos;
    private LocalMusicInfo localMusicInfo;

    public LocalMusicListAdapter (Context context, List<LocalMusicInfo> localMusicInfos){
        this.context = context;
        this.localMusicInfos = localMusicInfos;
    }

    @Override
    public int getCount() {
        return localMusicInfos != null && localMusicInfos.size() > 0 ? localMusicInfos.size() : 0;
    }

    @Override
    public LocalMusicInfo getItem(int position) {
        return localMusicInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.local_music_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.local_item_song = (TextView) convertView.findViewById(R.id.local_item_song);
            viewHolder.local_item_singer = (TextView) convertView.findViewById(R.id.local_item_singer);
            viewHolder.local_item_time = (TextView) convertView.findViewById(R.id.local_item_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        localMusicInfo = getItem(position);
        viewHolder.local_item_song.setText(localMusicInfo.getTitle());
        viewHolder.local_item_singer.setText(localMusicInfo.getArtist() + " - " + localMusicInfo.getAlbum());
        viewHolder.local_item_time.setText(String.valueOf(ChangeUtils.formatTime(localMusicInfo.getDuration())));
        return convertView;
    }

    public class ViewHolder {
        private TextView local_item_song;
        private TextView local_item_singer;
        private TextView local_item_time;
    }
}
